package com.ayf.payment.game.api.service.sign.handler;

import cn.hutool.core.util.IdcardUtil;
import com.ayf.payment.game.api.channel.alipay.internal.util.StringUtils;
import com.ayf.payment.game.api.dto.ayb.MerchantTwoDTO;
import com.ayf.payment.game.api.dto.ayb.SignPayProductDTO;
import com.ayf.payment.game.api.dto.platform.PaymentChannelDTO;
import com.ayf.payment.game.api.dto.sign.*;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantBank;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.MerchantBankService;
import com.ayf.payment.game.api.service.MerchantService;
import com.ayf.payment.game.api.service.SignPayProductService;
import com.ayf.payment.game.api.service.payment.ayb.MerchantTwoService;
import com.ayf.payment.game.api.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: zh-liang
 * @Date: 2022-03-11 15:55
 */
@Component
public class MerchantTwoAybSignHandlerImpl implements MerchantTwoSignHandler {

    @Autowired
    MerchantBankService merchantBankService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    MerchantTwoService merchantTwoService;

    @Autowired
    private SignPayProductService signPayProductService;

    @Override
    public MerchantTwoApplyResDTO merchantTwoApply(MerchantTwoApplyReqDTO reqDTO, PaymentChannelDTO channel) {
        MerchantTwoApplyResDTO merchantTwoApplyResDTO=new MerchantTwoApplyResDTO();
        String traceId = SnowFlakeUtils.getTraceId();
        Merchant merchant=merchantService.find(reqDTO.getMerchantId());
        if(merchant==null){
            throw new ParameterException("商户不存在");
        }
        MerchantTwoDTO merchantTwoDTO=new MerchantTwoDTO();
        MerchantBank merchantBank=merchantBankService.getMerchantBankByMid(reqDTO.getMerchantId());
        if(merchantBank==null){
            throw new ParameterException("请添加银行信息或审核验证");
        }
        if (StringUtils.isEmpty(merchantBank.getAccountName())) {
            throw new ParameterException("姓名不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCard())) {
            throw new ParameterException("身份证号不能为空");
        }
        if (!IdcardUtil.isValidCard(merchant.getIdCard())) {
            throw new ParameterException("身份证号格式错误");
        }
        if (StringUtils.isEmpty(merchant.getPhone())) {
            throw new ParameterException("手机号不能为空");
        }
        if (!CheckUtils.checkPhone(merchant.getPhone())) {
            throw new ParameterException("手机号格式错误");
        }
        if (StringUtils.isEmpty(merchant.getBusinessAddressProvinceCode())) {
            throw new ParameterException("所在省不能为空");
        }
        if (StringUtils.isEmpty(merchant.getBusinessAddressCityCode())) {
            throw new ParameterException("所在市不能为空");
        }
        if (StringUtils.isEmpty(merchant.getBusinessAddressAreaCode())) {
            throw new ParameterException("所在区不能为空");
        }
        if (StringUtils.isEmpty(merchant.getDetailAddress())) {
            throw new ParameterException("详细地址不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getAccountName())) {
            throw new ParameterException("开户名称不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getCardNo())) {
            throw new ParameterException("银行卡号不能为空");
        }
        if (!CheckUtils.checkBankCard(merchantBank.getCardNo())) {
            throw new ParameterException("银行卡号错误");
        }
        if (StringUtils.isEmpty(merchantBank.getBankProvinceCode())) {
            throw new ParameterException("开户省不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getBankCityCode())) {
            throw new ParameterException("开户市不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCardFrontImg())) {
            throw new ParameterException("身份证正面照不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCardBackImg())) {
            throw new ParameterException("身份证背面照不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getBankCardImg())) {
            throw new ParameterException("银行卡正面照不能为空");
        }
        SignPayProductDTO signPayProduct=signPayProductService.getByIdCardAndPayCode(merchant.getIdCard(),channel.getCode());
        if(signPayProduct !=null && signPayProduct.getMerchantId().intValue()!= reqDTO.getMerchantId().intValue()){
            throw new ParameterException("请先解约"+channel.getName()+"相同身份证商户");
        }
        merchantTwoDTO.setProfession("其他类");
        merchantTwoDTO.setSignedName(merchantBank.getAccountName());
        merchantTwoDTO.setZzLegalPersonIdNo(merchant.getIdCard());
        merchantTwoDTO.setPhone(merchantBank.getBankPhone());
        merchantTwoDTO.setBusinessAddressProvince(merchant.getBusinessAddressProvinceCode());
        merchantTwoDTO.setBusinessAddressCity(merchant.getBusinessAddressCityCode());
        merchantTwoDTO.setBusinessAddressArea(merchant.getBusinessAddressAreaCode());
        merchantTwoDTO.setBusinessAddress(merchant.getDetailAddress());
        merchantTwoDTO.setAccountName(merchantBank.getAccountName());
        merchantTwoDTO.setBankCardNo(merchantBank.getCardNo());
        merchantTwoDTO.setProvinceCode(merchantBank.getBankProvinceCode());
        merchantTwoDTO.setCityCode(merchantBank.getBankCityCode());

        merchantTwoDTO.setLegalIdCardProsFileByte(Base64.encode(FtpUtil.downloadXYFile("",merchant.getIdCardFrontImg())));
        merchantTwoDTO.setLegalIdCardProsOriginalFilename(merchant.getIdCardFrontImg());

        merchantTwoDTO.setLegalIdCardConsFileByte(Base64.encode(FtpUtil.downloadXYFile("",merchant.getIdCardBackImg())));
        merchantTwoDTO.setLegalIdCardConsOriginalFilename(merchant.getIdCardBackImg());

        merchantTwoDTO.setLegalPersonBankCardFileByte(Base64.encode(FtpUtil.downloadXYFile("",merchantBank.getBankCardImg())));
        merchantTwoDTO.setLegalPersonBankCardOriginalFilename(merchantBank.getBankCardImg());

        SignPayProductDTO signPayProductDTO=new SignPayProductDTO();
        signPayProductDTO.setMerchantId(reqDTO.getMerchantId());
        signPayProductDTO.setMerchantTwoDTO(merchantTwoDTO);

        CommonResult commonResult=merchantTwoService.apply(signPayProductDTO,traceId,channel);
        if(commonResult.getCode()==200){
            merchantTwoApplyResDTO.setCode(200);
        }else{
            throw new ParameterException(commonResult.getMsg());
        }
        return merchantTwoApplyResDTO;
    }

    @Override
    public MerchantTwoSelectResDTO merchantTwoSelect(MerchantTwoSelectReqDTO reqDTO, PaymentChannelDTO channel) {
        MerchantTwoSelectResDTO merchantTwoSelectResDTO=new MerchantTwoSelectResDTO();
        String traceId = SnowFlakeUtils.getTraceId();
        Merchant merchant=merchantService.find(reqDTO.getMerchantId());
        if(merchant==null){
            throw new ParameterException("商户不存在");
        }
        CommonResult commonResult=merchantTwoService.merchantQurey(reqDTO.getMerchantId(),traceId,channel);
        if(commonResult.getCode()==200){
            merchantTwoSelectResDTO.setCode(200);
            merchantTwoSelectResDTO.setStatus(commonResult.getMsg());
        }else{
            throw new ParameterException(commonResult.getMsg());
        }
        return merchantTwoSelectResDTO;
    }

    @Override
    public MerchantTwoUpdateResDTO merchantTwoUpdate(MerchantTwoUpdateReqDTO reqDTO, PaymentChannelDTO channel) {
        MerchantTwoUpdateResDTO merchantTwoUpdateResDTO=new MerchantTwoUpdateResDTO();
        String traceId = SnowFlakeUtils.getTraceId();
        Merchant merchant=merchantService.find(reqDTO.getMerchantId());
        if(merchant==null){
            throw new ParameterException("商户不存在");
        }
        MerchantTwoDTO merchantTwoDTO=new MerchantTwoDTO();
        MerchantBank merchantBank=merchantBankService.getMerchantBankByMid(reqDTO.getMerchantId());

        if (StringUtils.isEmpty(merchantBank.getAccountName())) {
            throw new ParameterException("姓名不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCard())) {
            throw new ParameterException("身份证号不能为空");
        }
        if (!IdcardUtil.isValidCard(merchant.getIdCard())) {
            throw new ParameterException("身份证号格式错误");
        }
        if (StringUtils.isEmpty(merchant.getPhone())) {
            throw new ParameterException("手机号不能为空");
        }
        if (!CheckUtils.checkPhone(merchant.getPhone())) {
            throw new ParameterException("手机号格式错误");
        }
        if (StringUtils.isEmpty(merchant.getBusinessAddressProvinceCode())) {
            throw new ParameterException("所在省不能为空");
        }
        if (StringUtils.isEmpty(merchant.getBusinessAddressCityCode())) {
            throw new ParameterException("所在市不能为空");
        }
        if (StringUtils.isEmpty(merchant.getBusinessAddressAreaCode())) {
            throw new ParameterException("所在区不能为空");
        }
        if (StringUtils.isEmpty(merchant.getDetailAddress())) {
            throw new ParameterException("详细地址不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getAccountName())) {
            throw new ParameterException("开户名称不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getCardNo())) {
            throw new ParameterException("银行卡号不能为空");
        }
        if (!CheckUtils.checkBankCard(merchantBank.getCardNo())) {
            throw new ParameterException("银行卡号错误");
        }
        if (StringUtils.isEmpty(merchantBank.getBankProvinceCode())) {
            throw new ParameterException("开户省不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getBankCityCode())) {
            throw new ParameterException("开户市不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCardFrontImg())) {
            throw new ParameterException("身份证正面照不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCardBackImg())) {
            throw new ParameterException("身份证背面照不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getBankCardImg())) {
            throw new ParameterException("银行卡正面照不能为空");
        }
        merchantTwoDTO.setProfession("其他类");
        merchantTwoDTO.setSignedName(merchantBank.getAccountName());
        merchantTwoDTO.setZzLegalPersonIdNo(merchant.getIdCard());
        merchantTwoDTO.setPhone(merchantBank.getBankPhone());
        merchantTwoDTO.setBusinessAddressProvince(merchant.getBusinessAddressProvinceCode());
        merchantTwoDTO.setBusinessAddressCity(merchant.getBusinessAddressCityCode());
        merchantTwoDTO.setBusinessAddressArea(merchant.getBusinessAddressAreaCode());
        merchantTwoDTO.setBusinessAddress(merchant.getDetailAddress());
        merchantTwoDTO.setAccountName(merchantBank.getAccountName());
        merchantTwoDTO.setBankCardNo(merchantBank.getCardNo());
        merchantTwoDTO.setProvinceCode(merchantBank.getBankProvinceCode());
        merchantTwoDTO.setCityCode(merchantBank.getBankCityCode());

        merchantTwoDTO.setLegalIdCardProsFileByte(Base64.encode(FtpUtil.downloadXYFile("",merchant.getIdCardFrontImg())));
        merchantTwoDTO.setLegalIdCardProsOriginalFilename(merchant.getIdCardFrontImg());

        merchantTwoDTO.setLegalIdCardConsFileByte(Base64.encode(FtpUtil.downloadXYFile("",merchant.getIdCardBackImg())));
        merchantTwoDTO.setLegalIdCardConsOriginalFilename(merchant.getIdCardBackImg());

        merchantTwoDTO.setLegalPersonBankCardFileByte(Base64.encode(FtpUtil.downloadXYFile("",merchantBank.getBankCardImg())));
        merchantTwoDTO.setLegalPersonBankCardOriginalFilename(merchantBank.getBankCardImg());

        SignPayProductDTO signPayProductDTO=new SignPayProductDTO();
        signPayProductDTO.setMerchantId(reqDTO.getMerchantId());
        signPayProductDTO.setMerchantTwoDTO(merchantTwoDTO);

        CommonResult commonResult=merchantTwoService.apply(signPayProductDTO,traceId,channel);
        if(commonResult.getCode()==200){
            merchantTwoUpdateResDTO.setCode(200);
        }else{
            throw new ParameterException(commonResult.getMsg());
        }
        return merchantTwoUpdateResDTO;
    }

    @Override
    public MerchantTwoUpdateSuperResDTO merchantTwoUpdateSuper(MerchantTwoUpdateSuperReqDTO reqDTO, PaymentChannelDTO channel) {
        MerchantTwoUpdateSuperResDTO merchantTwoUpdateSuperResDTO=new MerchantTwoUpdateSuperResDTO();
        String traceId = SnowFlakeUtils.getTraceId();
        Merchant merchant=merchantService.find(reqDTO.getMerchantId());
        if(merchant==null){
            throw new ParameterException("商户不存在");
        }
        CommonResult commonResult= merchantTwoService.merchantTwoUpdateSuper(reqDTO.getMerchantId(),traceId,channel);
        if(commonResult.getCode()==200){
            merchantTwoUpdateSuperResDTO.setCode(200);
          }else{
            throw new ParameterException(commonResult.getMsg());
        }
        return merchantTwoUpdateSuperResDTO;
    }

    @Override
    public boolean merchantTwoApplyAsyncExec(MerchantTwoApplyAsyncReqDTO reqDTO, PaymentChannelDTO channel) {
        return false;
    }
}
